/*
 * Create by erik on 18-8-15 下午2:05
 * Copyright (c) 2018. All rights reserved.
 *
 * Last Modified 18-8-15 下午2:05
 */

package com.feeyo.groundservice.fpush

import android.content.Context
import android.os.Build

object PushClient {

    private val tag = PushClient::class.java.simpleName
    val pushEngineList = ArrayList<BasePushEngine>()

    /**
     * 初始化推送
     */
    @Throws(IllegalStateException::class)
    fun init(context: Context) {
        if (pushEngineList.isEmpty())
            throw IllegalStateException("no push engine added")

        var engine = pushEngineList.find { it.name().equals(Build.BRAND, true) }

        if (engine == null) {
            engine = pushEngineList.find { it.name().equals("jiguang", true) }
        }

        if (null == engine)
            engine = pushEngineList.first()

        engine.init(context)
    }

    /**
     * 关闭推送
     */
    fun release(context: Context) {
        pushEngineList.forEach { it.release(context) }
        pushEngineList.clear()
    }

    /**
     * 增加推送引擎
     */
    fun addPushEngine(engine: BasePushEngine) {
        if (pushEngineList.find { it.name().equals(engine.name(), true) } != null) {
            println("has this push engine,${engine.name()}")
            return
        }

        pushEngineList.add(engine)
    }

    /**
     * 移除推送引擎
     */
    fun removePushEngine(name: String) {
        pushEngineList.remove(pushEngineList.find { name.equals(it.name(), true) })
    }
}